package com.zjj.filmcommentapi.service.impl;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zjj.filmcommentapi.model.User;
import com.zjj.filmcommentapi.service.RobotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RobotServiceImpl implements RobotService {
    @Autowired
    private RestTemplate restTemplate;

    public JSONObject postAPI(String text, User user) {
        JSONObject inputText = new JSONObject();
        inputText.put("text", text);
        JSONObject perception = new JSONObject();
        perception.put("inputText", inputText);
        JSONObject userInfo = new JSONObject();
        userInfo.put("apiKey", "388f811cd93442c99fd2bbc3e236b6a0");
        userInfo.put("userId", user.getAccount());
        JSONObject inData = new JSONObject();
        inData.put("perception", perception);
        inData.put("userInfo", userInfo);
        String url = "http://openapi.tuling123.com/openapi/api/v2";
        String outData = restTemplate.postForEntity(url, inData, String.class).getBody();
        return JSONObject.parseObject(outData);
    }

    public JSONObject getAPI(String text, User user) {
        String url = "http://api.qingyunke.com/api.php?key=free&appid=0&msg="+text;
        String outData = restTemplate.getForObject(url, String.class);
        String state =JSONObject.parseObject(outData) .getString("result");
        String content = JSONObject.parseObject(outData) .getString("content");
        JSONObject values = new JSONObject();
        values.put("text",content);
        Map map = new HashMap();
        map.put("groupType",1);
        map.put("resultType","text");
        map.put("values",values);
        List list = new ArrayList();
        list.add(map);
        JSONObject results = new JSONObject();
        results.put("results",list);
        System.out.println();
        return  results;
    }
}
